<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/23 0023
 * Time: 下午 22:41
 */
namespace app\admin\model;

use think\Db;
use think\Model;

class Admin extends Model
{
    protected $pk = 'id';

    public function addUser($data){
        $salt = $this->_randStr();
        $pass_word = $data['pass_word'].$salt;
        $user = $this->where('user_name',$data['user_name'])->find();
        if($user->user_name){
            return false;
        }
        $data = array(
            'user_name' => $data['user_name'],
            'email'     => $data['email'],
            'pass_word' => md5(md5($pass_word)),
            'salt'      => $salt,
            'nick_name' => $data['nick_name'] || 'henrick',
            'avatar'    => $data['avatar'] || 'sdsdf',
            'role_id'   => $data['role_id'],
            'role_list' => $data['role_list'],
            'create_time' => date("Y-m-d H:i:s"),
            'update_time' => date("Y-m-d H:i:s")
        );
        return $this->insert($data);
    }

    public function login($user_name,$pass_word){   //用户登录
        //$user = $this->where('user_name',$user_name)->find();

        $user = $this->field('a.*,b.name as role_name, b.auth_list')->alias('a')
            ->join('roles b','a.role_id = b.id','LEFT')
            ->where('user_name',$user_name)
            ->find();

        if(!$user->id){
            return false;
        }

        $pass_word = md5(md5($pass_word.$user->salt));
        if($pass_word!=$user->pass_word){
            return false;
        }

        //查询用户菜单
        $auth_list = $this->getUserAuth($user->auth_list,$user->role_id);
        cache('cache_admin_user_id_'.$user->id,$auth_list);
        session('admin_user_id',$user->id);
        $user_info = array(
            'id'        => $user->id,
            'user_name' => $user->user_name,
            'email'     => $user->email,
            'nick_name' => $user->nick_name,
            'avatar'    => $user->avatar,
            'role_id'   => $user->role_id,
            'role_name' => $user->role_name
        );
        session('admin_user_info',$user_info);
        unset($user,$user_info);
        return true;
    }

    public function getUserAuth($auth_list,$role_id){
        $auth = $this->table($this->getTable('auth'))
            ->field('id,name as cn_name,css_icon,module,controller,action,param,pid,is_menu,menu_level')
            ->order('menu_level,weight','asc')->select();

        $user_auth = [];
        $user_menu = [];
        foreach ($auth as $key=>$val){
            if(strstr($auth_list,','.$val->id.',') || $role_id==1){ //暂时不做参数Url解析
                $url = $val->module.'/'.$val->controller.'/'.$val->action;
                $user_auth[] = $url;
                //判断是否是菜单
                if(!$val->is_menu){
                    continue;
                }

                $menu = array(
                    'id'       => $val->id,
                    'name'     => $val->cn_name,
                    'url'      => $url,
                    'css_icon' => $val->css_icon
                );

                //判断是否是一级菜单
                if($val->menu_level==0){
                    $user_menu[$val->id]   =  $menu;
                    continue;
                }

                //判断是否为子菜单
                $pid = explode('-',$val->pid);
                $doVal = $user_menu;
                if(count($pid)==1){
                    $user_menu[$pid[0]]['sub_menu'][$val->id] = $menu;
                }else if(count($pid)==2){
                    $user_menu[$pid[0]][$pid[1]]['sub_menu'][$val->id] = $menu;
                }else if(count($pid)==3){
                    $user_menu[$pid[0]][$pid[1]][$pid[2]]['sub_menu'][$val->id] = $menu;
                }
                unset($doVal);
            }
        }
        return [
            'user_auth' => $user_auth,
            'user_menu' => $user_menu
        ];
    }

    protected function _randStr($pw_length = 10){
        $randStr ="";
        for ($i = 0; $i < $pw_length; $i++)
        {
            $randStr .= chr(mt_rand(33, 126));
        }
        return $randStr;
    }


}